using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for AlertsControl.
	/// </summary>
	public class AlertsControl : System.Windows.Forms.UserControl
	{
    private FreeFlowAdministrator.ExtendedDataGrid alertsGrid;
    private IContainer components;

		public AlertsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      actionList = new UIActionList();
      actionList.Add("Open selected folders", "Open the selected folders", 
        new ActionExecuteDelegate(OpenFolders));
      actionList.Add("Copy to clipboard", "Copy the alerts list to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.alertsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.alertsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // alertsGrid
      // 
      this.alertsGrid.AllowNavigation = false;
      this.alertsGrid.CaptionVisible = false;
      this.alertsGrid.DataMember = "";
      this.alertsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.alertsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.alertsGrid.Location = new System.Drawing.Point(0, 0);
      this.alertsGrid.Name = "alertsGrid";
      this.alertsGrid.Size = new System.Drawing.Size(488, 384);
      this.alertsGrid.TabIndex = 0;
      this.alertsGrid.OpenSelected += new System.EventHandler(this.alertsGrid_OpenSelected);
      // 
      // AlertsControl
      // 
      this.Controls.Add(this.alertsGrid);
      this.Name = "AlertsControl";
      this.Size = new System.Drawing.Size(488, 384);
      ((System.ComponentModel.ISupportInitialize)(this.alertsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    public void LoadAlerts(AlertCollection alerts)
    {
      alertsGrid.DataSource = alerts;
      alertsGrid.ClearColumns();
      alertsGrid.AddColumn("FolderName", "Folder name");
      alertsGrid.AddColumn("FolderId", "Folder ID");
      alertsGrid.AddColumn("Subject");
      alertsGrid.AddColumn("Message");
      alertsGrid.AddColumn("Updated");
      alertsGrid.AddColumn("Deadline");
      alertsGrid.AddColumn("Priority");
      alertsGrid.AddColumn("MapName", "Map");
      alertsGrid.AddColumn("StageName", "Stage");
      alertsGrid.AddColumn("Category");
      alertsGrid.AutosizeColumns();
    }

    private string OpenFolders()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)alertsGrid.BindingContext[alertsGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(alertsGrid.IsSelected(i))
          {
            Alert alert = (Alert)bm.List[i];
            Folder folder = new Folder(alert.Server, alert.FolderId);
            FolderForm form = new FolderForm();
            form.LoadFolder(folder);
            form.Show();
          }
        }
      }
      return null;
    }

    private string CopyToClipboard()
    {
      alertsGrid.CopyToClipboard();
      return null;
    }

    private UIActionList actionList;
    public UIActionList ActionList
    {
      get
      {
        return actionList;
      }
    }

    private void alertsGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenFolders();
    }
	}
}
